"""
    节流
    API 访问次数的限制
    防止爬虫过度访问
    一般通过用户的ID或者IP当作唯一的keys
"""

from rest_framework.throttling import SimpleRateThrottle


class VisitThrottling(SimpleRateThrottle):
    scope = "未认证用户"

    def get_cache_key(self, request, view):
        return self.get_ident(request)     # 用当前访问的 IP 当作 keys


class UserThrottling(SimpleRateThrottle):
    scope = "已认证用户"

    def get_cache_key(self, request, view):
        return request.user     # 用当前登陆的用户当作 keys


